﻿Public Class CreateCasesForm

    Dim CasesView As DataView



    Public Sub LoadCaseFieldsBox()
      

        CasesView = LoadDataView("Select * from Modelling_TableList union all select 'Modelling_Products' as Table_Name, 'Products' as Table_Description")
        'CasesView = LoadDataView("Select * from Modelling_TableList")
        ActTableBox.DataSource = CasesView
        ActTableBox.DisplayMember = "Table_Description"
        ActTableBox.ValueMember = "Table_Name"

        CasesView = LoadDataView("Select * from Modelling_TableList")
        'CasesView = LoadDataView("Select * from Modelling_TableList")
        TableNameBox.DataSource = CasesView
        TableNameBox.DisplayMember = "Table_Description"
        TableNameBox.ValueMember = "Table_Name"

        If TableNameBox.SelectedValue.ToString = "Modelling_Factors" Then
            ActTableBox.SelectedValue = "Modelling_Factors"
            ActTableBox.Enabled = False
        Else
            ActTableBox.Enabled = True
        End If

    End Sub

    Public Sub LoadDataCutBox()

        If IsNothing(ActTableBox.SelectedValue) Then Exit Sub

        If ActTableBox.SelectedValue.ToString = "System.Data.DataRowView" Then Exit Sub


        If ActTableBox.SelectedValue.ToString = "Modelling_Products" Then
            CasesView = LoadDataView("select m.Scenario_ID + cast(Product_Number as nvarchar(max)) as DataCut_ID, Product_Description + ' (' +  rtrim(Scenario_Description) + ')' as Description from Modelling_Products p, Modelling_Scenarios m where p.Scenario_ID = m.Scenario_ID and (m.User_Name = '" & CurrentUser.Name & "' or m.Published = 1) order by m.Scenario_ID, Product_Description")
        Else
            CasesView = LoadDataView("select distinct DataCut_ID, Description from " & ActTableBox.SelectedValue.ToString & " where Source not like  'Case%' order by Description")
        End If

        DataCutBox.DataSource = CasesView
        DataCutBox.DisplayMember = "Description"
        DataCutBox.ValueMember = "DataCut_ID"

    End Sub

    Private Sub btn_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OK.Click
        'make sure at least one field is selected
  

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub


    Private Sub CreateCasesForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = MainForm.Icon

    End Sub

    Public Sub LoadBudgetBox()
        Dim sql As String

        sql = "select 'NA' as Budget_ID, 'NA' as Budget_Description union all select Budget_ID, Budget_Description from Budgets"
        BudgetBox.DataSource = LoadDataView(sql)
        BudgetBox.DisplayMember = "Budget_Description"
        BudgetBox.ValueMember = "Budget_ID"

    End Sub

    Private Sub ActTableBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActTableBox.SelectedIndexChanged
        LoadDataCutBox()

    End Sub

    Private Sub TableNameBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TableNameBox.SelectedIndexChanged
        If TableNameBox.SelectedValue.ToString = "Modelling_Factors" Then
            ActTableBox.SelectedValue = "Modelling_Factors"
            ActTableBox.Enabled = False
        Else
            ActTableBox.Enabled = True
        End If
    End Sub
End Class